package com.yu.gulimall.product.controller;

import com.yu.common.utils.PageUtils;
import com.yu.common.utils.R;
import com.yu.gulimall.product.entity.AttrEntity;
import com.yu.gulimall.product.entity.AttrGroupEntity;
import com.yu.gulimall.product.service.AttrGroupService;
import com.yu.gulimall.product.vo.AttrGroupWithAttrVO;
import com.yu.gulimall.product.vo.AttrVo;
import com.yu.gulimall.product.vo.RelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author yu
 * @email yu@gmail.com
 * @date 2024-02-27 15:13:51
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);


        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogIds = attrGroupService.findParentPath(catelogId);
        attrGroup.setCatelogIds(catelogIds);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 获取分类属性分组
     * @param catelogId
     * @param params
     * @return
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:delete")
    public R getGroupByCatelogId(@PathVariable Long catelogId, @RequestParam Map<String, Object> params) {

        PageUtils page = attrGroupService.getGroupWithCatelogId(catelogId, params);

        return R.ok().put("page", page);
    }


    @GetMapping("/{attrgroupId}/attr/relation")
    public R getGroupRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> data=attrGroupService.getRelationByAttrgroupId(attrgroupId);

        return R.ok().put("data",data);

    }


    /**
     * /product/attrgroup/attr/relation/delete
     * 删除属性与分组的关联关系
     * @param params
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrVo[] params){
        attrGroupService.deleteWithId(params);
        return R.ok();
    }


    /**
     *     /product/attrgroup/{attrgroupId}/noattr/relation
     *
     *
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getOtherRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String, Object> params){

        PageUtils page=attrGroupService.getOtherRelationByAttrgroupId(attrgroupId,params);
        return R.ok().put("page",page);
    }

    /**
     * 添加属性与分组关联关系
     * @param relationVOList
     * @return
     */

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody RelationVO[] relationVOList){
        attrGroupService.addAttrRelation(relationVOList);
        return R.ok();
    }

    /**
     * 17、获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */

    @GetMapping("/{catelogId}/withattr")
    public R getWithAttr(@PathVariable Long catelogId){
        List<AttrGroupWithAttrVO> data=attrGroupService.getAttrgroupWithAttr(catelogId);
        return R.ok().put("data",data);
    }

}
